#ifndef CHATWINDOW_H
#define CHATWINDOW_H

#include <QMainWindow>
#include <QListWidgetItem>
#include"client_network.h"
namespace Ui {
class ChatWindow;
}

class ChatWindow : public QMainWindow
{
    Q_OBJECT

signals:
    void logoutRequested();

public:
    explicit ChatWindow(ClientNetwork *clientNet,QWidget *parent = nullptr);
    ~ChatWindow();
    QString username;

private slots:
    void onFontChanged(const QFont &font);
    void onFontSizeChanged(int index);
    void onBoldToggled(bool checked);
    void onItalicToggled(bool checked);
    void onUnderlineToggled(bool checked);
    void onColorClicked();
    void onEmojiClicked();
    void onImageClicked();
    void onFileClicked();
    void onSendClicked();
    void onChatModeChanged(int index);
    void onUserItemClicked(QListWidgetItem *item);
    void updateOnlineUsers(const QJsonArray &users);
    void onPrivateMessageReceived(const QJsonObject &message);
    void onGroupMessageReceived(const QJsonObject &message);
    void onTransferMessageReceived(const QJsonObject &message, bool isImage);
    void onAnchorClicked(const QUrl &link);

private:
    Ui::ChatWindow *ui;
    ClientNetwork *m_clientNetwork;
    QMap<QString, QByteArray> cachedFiles;
    void setupConnections();
    void addUser(const QString &name, const QIcon &icon);
    void appendMessageToChat(const QString &sender,
                             const QString &receiver,
                             const QString &message,
                             const QDateTime &timestamp,
                             bool isPrivateChat,
                             bool isMyMessage);
    void displayImageMessage(const QString &sender, const QString &receiver,
                             const QImage &image, const QString &fileName,
                             bool isMyMessage);
    void displayFileMessage(const QString &sender, const QString &receiver,
                             const QString &fileName, qint64 fileSize,
                             bool isMyMessage);
    void shakeWindow();
    void saveReceivedFile(const QString &fileName, const QByteArray &fileData);
    void saveReceivedImage(const QString &fileName, const QByteArray &imageData);
    QByteArray getCachedFileData(const QString &fileName);
    void saveFileToDisk(const QString &fileName, const QByteArray &data);
};

#endif // CHATWINDOW_H
